package com.pdstudio.carrecom.ui.fragment.account;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.http.RequestParams;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.app.UIHelper;
import com.pdstudio.carrecom.bean.CarType;
import com.pdstudio.carrecom.bean.CarType.car;
import com.pdstudio.carrecom.bean.PhoneValideCode;
import com.pdstudio.carrecom.bean.ResultInfo;
import com.pdstudio.carrecom.tools.JsonUtil;
import com.pdstudio.carrecom.ui.activity.account.ActivityGetPassword;
import com.pdstudio.carrecom.ui.activity.account.ActivityGoRegister;

/**
 * 注册第一步
 * 类: FragmentRegisterS1 <br>
 * 描述: TODO <br>
 * 作者: xiangzhaoyu  <br>
 * 时间: 2015-5-18 上午11:22:45
 */
public class FragmentRegisterS1 extends Fragment {
	
	private View _view;
	private Activity _context;
	private Button _nextStep,mGetValideCode;
	private EditText mPhone,mValidecode;
	private Spinner mSelectCar;
	private ArrayAdapter<String> mSelectCarAd;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		_view = inflater.inflate(R.layout.fragment_account_register_s1, container,false);
		_context = getActivity();
		initialView();
		return _view;
	}
	
	private void initialView(){
		
		//获取车辆列表
		try {
			HttpExecuteJson http = new HttpExecuteJson(_context, mGetCarListListener);
			Map<String,Object> rps=new HashMap<String,Object>();
			http.get(ServiceHelper.CarList, rps);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		_nextStep=(Button)_view.findViewById(R.id.btn_nextstep);
		_nextStep.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mValidecode.getText().toString().equals("")||mValidecode.getText()==null){
					UIHelper.ToastMessage(_context, "请先获取验证码！");
				}else{
					ActivityGoRegister agr= (ActivityGoRegister)_context;
					agr.openFragment(new FragmentRegisterS2());
				}
			}});
		
		mPhone=(EditText)_view.findViewById(R.id.et_phonenum);
		mValidecode=(EditText)_view.findViewById(R.id.et_validecode);
		mGetValideCode=(Button)_view.findViewById(R.id.btn_getvalidecode);
		mGetValideCode.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					if(mPhone.getText().toString().equals("")||mPhone.getText()==null){
						UIHelper.ToastMessage(_context, "手机号不能为空！");
					}else{
						HttpExecuteJson http = new HttpExecuteJson(_context, mLoginListener);
						RequestParams rps=new RequestParams();
						http.post(ServiceHelper.RegisterCode+"/"+mPhone.getText().toString(), rps);
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}});
		
		mSelectCar=(Spinner)_view.findViewById(R.id.spin_ac_re_selectcar);
		
	}
	
	/*
	 * 获取验证码*/
	private void DoJson(String json){
		PhoneValideCode edv = null;
		try {
			edv = new Gson().fromJson(json,
					new TypeToken<PhoneValideCode>() {
					}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				//原本在initview的代码放在这下面
				try{
					mValidecode.setText(edv.code);

					//记录用户的电话号码跟车型
					ActivityGoRegister agr=(ActivityGoRegister)_context;
					agr.mPhoneNum=mPhone.getText().toString();
					agr.mCar=mSelectCar.getSelectedItem().toString();
				}catch(Exception e){
					e.printStackTrace();
				}
			}else{
				UIHelper.ToastMessage(_context, "获取验证码失败");
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			UIHelper.ToastMessage(_context, "获取验证码失败："+e.getMessage());
		}

	}
	
	/*
	 * 获取验证码监听*/
	private httpReturnJson mLoginListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(_context, "获取验证码失败" + msg);
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(_context, "获取验证码失败");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJson(result);
		}
	};
	
	/*
	 * 获取车辆数据*/
	private void DoJsonSc(String json){
		//String[] items={"我的爱车是：","大众","奔驰","宝马","奥迪","标致"};
		ResultInfo edv = null;
		try {
			edv = new Gson().fromJson(json,
					new TypeToken<ResultInfo>() {
					}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				//原本在initview的代码放在这下面
				try{
//					List<car> cars=(ArrayList<car>)edv.data;
					
					List<car> cars = JsonUtil.fromJsonArray(edv.data.toString(), car.class);
					
					String[] items=new String[cars.size()+1];
					car c=(car)cars.get(0);
					items[0]=c.name;
					for(int i=1;i<(cars.size()+1);i++){
						items[i]=c.name;
					}
					
					mSelectCarAd=new ArrayAdapter<String>(_context,android.R.layout.simple_spinner_item,items);
					mSelectCar.setAdapter(mSelectCarAd);
				}catch(Exception e){
					e.printStackTrace();
					UIHelper.ToastMessage(_context, "获取车辆列表失败");
				}
			}else{
				UIHelper.ToastMessage(_context, "获取车辆列表失败");
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			UIHelper.ToastMessage(_context, "获取车辆列表失败");
		}

		
	}

	private httpReturnJson mGetCarListListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(_context, "获取车辆列表失败" + msg);
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(_context, "获取车辆列表失败");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJsonSc(result);
		}
	};

	
}
